package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.OrderDetail;
import com.itheima.bean.Orders;
import com.itheima.bean.ShoppingCart;
import com.itheima.commons.BaseContext;
import com.itheima.commons.R;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @date 2023/1/12
 */
@Slf4j
@RequestMapping("/order")
@RestController
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 下单操作
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        //1.调用service的下单方法
        ordersService.submit(orders);
        //2.返回结果
        return R.success("下单成功！");
    }

    @GetMapping("/page")
    public R<IPage<OrdersDto>> page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "1") Integer pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        IPage<Orders> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper();
        lqw.like(number != null,Orders::getId,number);
        lqw.between(beginTime != null && endTime != null,Orders::getOrderTime ,beginTime,endTime);
        ordersService.page(iPage,lqw);

        IPage<OrdersDto> iPage1 = new Page<>();


        List<Orders> orders = iPage.getRecords();

        List<OrdersDto> ordersDtos = new ArrayList<>();

        orders.forEach(o -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(o,ordersDto);

            //去拿订单信息
            LambdaQueryWrapper<OrderDetail> l = new LambdaQueryWrapper<>();
            l.eq(OrderDetail::getOrderId,o.getId());
            ordersDto.setOrderDetails(orderDetailService.list(l));
            ordersDtos.add(ordersDto);
        });

        iPage1.setTotal(iPage.getTotal());
        iPage1.setSize(iPage.getSize());
        iPage1.setRecords(ordersDtos);


        return R.success(iPage1);
    }

    /**
     * 更新订单状态(后台)
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> upStatus(@RequestBody Orders orders){
        return ordersService.updateById(orders) ? R.success("更新成功") : R.error("更新失败");
    }

    /**
     * 分页查询用户订单,员工后台功能
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<IPage<OrdersDto>> order(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "1") Integer pageSize){
        IPage<Orders> iPage = new Page<>(page,pageSize);
        ordersService.page(iPage);

        IPage<OrdersDto> iPage1 = new Page<>();


        List<Orders> orders = iPage.getRecords();

        List<OrdersDto> ordersDtos = new ArrayList<>();

        orders.forEach(o -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(o,ordersDto);

            //去拿订单信息
            LambdaQueryWrapper<OrderDetail> l = new LambdaQueryWrapper<>();
            l.eq(OrderDetail::getOrderId,o.getId());
            ordersDto.setOrderDetails(orderDetailService.list(l));
            ordersDtos.add(ordersDto);
        });

        iPage1.setTotal(iPage.getTotal());
        iPage1.setSize(iPage.getSize());
        iPage1.setRecords(ordersDtos);


        return R.success(iPage1);
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        //得到订单id
        //去订单详情表查菜品
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,orders.getId());

        List<OrderDetail> list = orderDetailService.list(lqw);
        List<ShoppingCart> shoppingCarts = new ArrayList<>();
        Long userId = BaseContext.getCurrentId();
        list.forEach(orderDetail -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(orderDetail.getName());
            shoppingCart.setImage(orderDetail.getImage());
            shoppingCart.setDishId(orderDetail.getDishId());
            shoppingCart.setSetmealId(orderDetail.getSetmealId());
            shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
            shoppingCart.setNumber(orderDetail.getNumber());
            shoppingCart.setAmount(orderDetail.getAmount());
            shoppingCart.setUserId(userId);
            shoppingCarts.add(shoppingCart);
        });

        return shoppingCartService.saveBatch(shoppingCarts) ? R.success("") : R.error("加入购物车失败");
    }
}
